# 4  编写装饰器来实现，对目标函数进行装饰,计算函数的运行耗时，
#     分3种情况：目标函数无参数无返回值，目标函数有参数，目标函数有返回值；
#      三个目标函数分别为：
#      A 打印输出20000之内的素数；
#      B 计算整数2-10000之间的素数的个数；
#      C 计算整数2-M之间的素数的个数；



import time


def time_value(dec):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        get_str = dec(*args, **kwargs)
        end_time = time.time()
        print("函数运行共耗时：", end_time - start_time)
        return get_str

    return wrapper


@time_value
def test1():
    i = 2
    while i <= 20000:
        j = 2
        flag = True
        while j <= i / 2:
            if i % j == 0:
                flag = False
            j = j + 1
        if flag:
            print(i)
        i = i + 1


@time_value
def test2():
    i = 2
    sum1 = 0
    while i <= 10000:
        j = 2
        flag = True
        while j <= i / 2:
            if i % j == 0:
                flag = False
            j = j + 1
        if flag:
            print(i)
            sum1 = sum1 + 1
        i = i + 1
    return sum1


@time_value
def test3(m):
    i = 2
    sum1 = 0
    while i <= m:
        j = 2
        flag = True
        while j <= i / 2:
            if i % j == 0:
                flag = False
            j = j + 1
        if flag:
            sum1 = sum1 + 1
        i = i + 1
    return sum1


print(test1)
print(test2())
print(test3(10000))
